package com.geodroid.euclidian;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;

import com.geodroid.android.graphics.SegmentF;
import com.geodroid.common.kernel.geos.GeoPoint2;
import com.geodroid.common.kernel.geos.GeoSegment;

public class DrawSegment extends Drawable {

	private String TAG = "DrawSegment" ; 
	

	private SegmentF line ;
	public long priority = 100;
	
	public DrawSegment(GeoSegment geop,EuclidianView v){
		super(geop, v) ;
		line = new SegmentF(); 
		update() ;
	}
	

//	@Override
//	public float distance(float x, float y) {
//		// TODO Auto-generated method stub
//		float d = (float) (Math.abs((a*x+b*y+c))/Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)));
//		Log.d("DrawSegment", "<distance = " + d)  ;
//		return d ;  
//	}

	@Override
	public long getDrawingPriority() {
		// TODO Auto-generated method stub
		return priority;
	}
	
//	@Override
//	public boolean hits(float x, float y) {
//		Log.d("DrawSegment", "<hits>")  ;
//		return distance(x,y)<25 ;
//	}

	
	
	

	@Override
	public void draw(Canvas canvas) {
		Log.d(TAG,"<draw>") ; 
		update() ;
		paint.setStyle(Paint.Style.FILL);
	
		if (hightLighted){
			paint.setStrokeWidth(3);
			paint.setColor(Color.RED);
			canvas.drawLine(line.startX, line.startY, line.stopX, line.stopY,paint) ;
		}

		paint.setStrokeWidth(2);
		paint.setColor(Color.BLUE);

		canvas.drawLine(line.startX, line.startY, line.stopX, line.stopY,paint) ;
		Log.d(TAG,"</draw>") ; 
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		float startX = view.toScreenCoordX(((GeoSegment) getGeo()).getStartPoint().inhomX )  ; 
		float stopX = view.toScreenCoordX(((GeoSegment) getGeo()).getEndPoint().inhomX ) ;
		float startY = view.toScreenCoordY(((GeoSegment) getGeo()).getStartPoint().inhomY) ; 
		float stopY = view.toScreenCoordY(((GeoSegment) getGeo()).getEndPoint().inhomY) ;
		line.set(startX, startY, stopX, stopY) ; 
	}





	@Override
	public boolean hits(RectF touchedArea) {
		return line.intersect(touchedArea);
	}


	@Override
	public void translate(double ux, double uy) {
//		double rwX = view.toRealWorldCoordX(ux) ; 
//		double rwY = view.toRealWorldCoordY(uy) ;
		double rwX = ux*view.getInvXscale() ;
		double rwY = -uy*view.getInvYscale() ;
		
		GeoSegment s = (GeoSegment) getGeoElement() ;
		GeoPoint2 start = s.getStartPoint() ; 
		start.setCoords(start.inhomX + rwX, start.inhomY + rwY) ; 
		GeoPoint2 end = s.getEndPoint() ; 
		end.setCoords(end.inhomX + rwX, end.inhomY + rwY) ;
		start.updateCascade() ;
		end.updateCascade() ;
		update() ;
	}




}
